//
//  LGTRWZPostOptionCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/23.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZPostOptionCell: UITableViewCell {

    var topTextView:LGTInputTextView?
    var moneyAllLabel:UILabel?
    var returnStrBlock:ClickCellStrBlock?  //返回给j交互视图
    var textChangeBlock:ClickReturnStrBlock?  //键盘文字变化回掉处理
    private var _model:Dictionary<String, Any>?
    var path:IndexPath?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        let titleLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: YellowColor, fontSize: SizeFitFloat(15), text: "选填项")
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(14))
            make.height.equalTo(SizeFit(15))
            make.top.equalTo(SizeFit(18))
        }
        
        let label:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "置顶天数")
        self.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(14))
            make.height.equalTo(SizeFit(15))
            make.top.equalTo(SizeFit(48))
        }
        
        self.topTextView = LGTViewCreateUtil.share.createInputTextView(placehoder: "", fontSize:  SizeFitFloat(15), limitCout:10)
        self.topTextView?.textView.keyboardType = UIKeyboardType.numberPad
        self.addSubview(self.topTextView!)
        self.topTextView?.snp.makeConstraints({ (make) in
            make.top.equalTo(SizeFit(48))
            make.width.equalTo(SizeFit(100))
            make.height.equalTo(SizeFit(25))
            make.left.equalTo(SizeFit(95))
        })
        
        self.moneyAllLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.right, textColor: YellowColor, fontSize: SizeFitFloat(15), text: "0元")
        self.addSubview(self.moneyAllLabel!)
        self.moneyAllLabel?.snp.makeConstraints({ (make) in
            make.right.equalTo(SizeFit(-12))
            make.height.equalTo(SizeFit(15))
            make.top.equalTo(SizeFit(48))
            
        })
        
        let topdescLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.right, textColor: TextColor, fontSize: SizeFitFloat(15), text: "置顶费用")
        self.addSubview(topdescLabel)
        topdescLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self.moneyAllLabel!.snp_leftMargin).offset(-SizeFitFloat(26))
            make.height.equalTo(SizeFit(15))
            make.top.equalTo(SizeFit(48))
        }
        
        let line:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.top.equalTo(85)
            make.height.equalTo(1)
            make.left.equalTo(SizeFitFloat(13))
            make.right.equalTo(SizeFitFloat(-11))
            make.bottom.equalTo(0)
        }
        
        //值变化回掉
        weak  var weakSelf = self
        self.textChangeBlock = {
            (text:String) -> Void in
            weakSelf?.updateMoney(text: text)
            if weakSelf?.returnStrBlock != nil{
                weakSelf?.returnStrBlock!(text, weakSelf!.path!)
            }
        }
        self.topTextView?.returnReturnStrBlock = self.textChangeBlock
    }
    
    func updateMoney(text:String){
        let times:Int = Int(text) ?? 0
        //一天多少钱
        let topTimeMoney:Int = self.model["topTimeMoney"] as? Int ?? 0
        let money:String = "\(Double(times * topTimeMoney)/100.0)元"
        self.moneyAllLabel?.text = money
    }
    
    func setData(){
        //"toptime":topTimeStr, "topTotalMoney": topTotalMoney
        self.moneyAllLabel?.text = self.model["topTotalMoney"] as? String
        self.topTextView?.textView.text = self.model["toptime"] as? String
    }
}
